package com.uniriho.lxr0811_7300_bl.ui.uhf

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.uniriho.lxr0811_7300_bl.base.Constant
import com.uniriho.lxr0811_7300_bl.databinding.FragmentUhfBinding
import com.uniriho.lxr0811_7300_bl.utils.DataTo
import com.uniriho.lxr0811_7300_bl.utils.packetHandles

class Fragment_UHF : Fragment(), View.OnClickListener {

    lateinit var result: TextView
    lateinit var UHFVsBtn: Button
    lateinit var UHFFindBtn: Button
    lateinit var UHFReadBtn: Button
    lateinit var UHFWriteBtn: Button
    lateinit var UHFWritePowerBtn: Button
    lateinit var UHFReadPowerBtn : Button
    lateinit var UHFWriteAnBtn   : Button
    lateinit var UHFReadAnBtn    : Button

    private var ufhBinding: FragmentUhfBinding? = null
    private val viewmodelUhf by lazy {
        ViewModel_UHF()
    }

    // This property is only valid between onCreateView and
    // onDestroyView.

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        ufhBinding = FragmentUhfBinding.inflate(inflater, container, false)
        val root: View = ufhBinding!!.root
        initView()
        initData()
        initEvent()
        return root
    }

    private fun initEvent() {
        result.setOnClickListener(this)
        UHFVsBtn.setOnClickListener(this)
        UHFFindBtn.setOnClickListener(this)
        UHFReadBtn.setOnClickListener(this)
        UHFWriteBtn.setOnClickListener(this)
        UHFWritePowerBtn.setOnClickListener(this)
        UHFReadPowerBtn .setOnClickListener(this)
        UHFWriteAnBtn   .setOnClickListener(this)
        UHFReadAnBtn    .setOnClickListener(this)

    }

    private fun initData() {


    }

    private fun initView() {
        result = ufhBinding!!.textResult
        viewmodelUhf.testResult.observe(viewLifecycleOwner) {
            result.text = it

        }
        UHFVsBtn = ufhBinding!!.btnUHFVS
        UHFFindBtn = ufhBinding!!.btnUHFFindCar
        UHFReadBtn = ufhBinding!!.btnUHFRead
        UHFWriteBtn = ufhBinding!!.btnUHFWrite
        UHFWritePowerBtn = ufhBinding!!.btnSetPower
        UHFReadPowerBtn  = ufhBinding!!.btnReadPower
        UHFWriteAnBtn    = ufhBinding!!.btnSetAn
        UHFReadAnBtn     = ufhBinding!!. btnReadAn

    }

    override fun onDestroyView() {
        super.onDestroyView()
        ufhBinding = null
    }


    override fun onClick(p0: View?) {
        when (p0) {
            ufhBinding!!.btnUHFVS -> {
                viewmodelUhf.sendData(Constant.UHF_VS)
            }
            ufhBinding!!.btnUHFFindCar -> {
                viewmodelUhf.sendData(Constant.UHF_FIND)
            }
            ufhBinding!!.btnUHFRead -> {
                viewmodelUhf.UHFRead()
            }
            ufhBinding!!.btnUHFWrite -> {

                viewmodelUhf.UHFWrite()
            }

            ufhBinding!!.btnSetPower -> {
                viewmodelUhf.sendData(Constant.UHF_WRITEPOWER)
            }
            ufhBinding!!.btnReadPower -> {
                viewmodelUhf.sendData(Constant.UHF_READPOWER)
            }
            ufhBinding!!.btnSetAn -> {
                viewmodelUhf.UHFSetAn()
            }
            ufhBinding!!.btnReadAn -> {
                viewmodelUhf.sendData(Constant.UHF_READANTENNA)
            }
        }
    }
}